//
//  CXHTopOnNativeEvent.m
//  CXHAdSDK-TopOn
//
//  Created by 陈坤 on 2024/10/14.
//

#import "CXHTopOnNativeEvent.h"
#import <CXHAdSDK-Adapter/CXHAdapterNativeAdViewDelegate.h>
#import <CXHAdSDK-Adapter/CXHAdapterNativeAdData.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>
#import "CXHTopOnCommonBiddingManager.h"
#import "CXHTopOnCommonBiddingRequest.h"
#import "CXHTopOnCommonAdapter.h"
#import "CXHTopOnNativeObject.h"

@implementation CXHTopOnNativeEvent


- (void)nativeSelfRenderAdLoadedWith:(CXHAdSDKNativeAd *)nativeAd nativeAdModel:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)nativeAdView viewController:(UIViewController *)viewController{
    
    [nativeAdView cxh_setRootViewController:viewController];
    
    NSMutableArray<NSDictionary*>* assets = [NSMutableArray<NSDictionary*> array];
    NSMutableDictionary *asset = [NSMutableDictionary dictionary];
    
    CXHAdapterNativeAdData *adData = [nativeAdView cxh_nativeAdData];
    
    UIView *logoView = [nativeAdView cxh_logoView];
    
    [asset CXH_setValidValue:self forKey:kATAdAssetsCustomEventKey];
    [asset CXH_setValidValue:@(0) forKey:kATNativeADAssetsIsExpressAdKey];
    [asset CXH_setValidValue:nativeAdView forKey:kATAdAssetsCustomObjectKey];
    
    [asset CXH_setValidValue:adData.title forKey:kATNativeADAssetsMainTitleKey];
    [asset CXH_setValidValue:adData.desc forKey:kATNativeADAssetsMainTextKey];
    [asset CXH_setValidValue:adData.iconImageUrl forKey:kATNativeADAssetsIconURLKey];
    [asset CXH_setValidValue:adData.imageUrl forKey:kATNativeADAssetsImageURLKey];
    [asset CXH_setValidValue:logoView forKey:@"kCXHLogoView"];
    
    
    [asset CXH_setValidValue:@(0) forKey:kATNativeADAssetsContainsVideoFlag];
    [asset CXH_setValidValue:@(nativeAd.getBidPrice) forKey:kATNativeADAssetsAppPriceKey];
    [asset CXH_setValidValue:nativeAd forKey:@"kCXHNativeSelfRenderObj"];
    
    [assets addObject:asset];
    [self trackNativeAdLoaded:assets];
}

#pragma mark - CXHAdSDKNativeAdDelegate

/**
 信息流原生广告请求成功

 @param nativeAd 广告实例
 @param adViewArray 广告模板集合
 */
- (void)CXH_nativeAdSucessToLoad:(CXHAdSDKNativeAd *)nativeAd adViewArray:(NSArray<__kindof UIView<CXHAdapterNativeAdViewDelegate> *> *)adViewArray{
    NSDictionary *infoDic = [CXHTopOnCommonAdapter getC2SInfo:[nativeAd getBidPrice]];
    
    NSMutableArray *assets = [NSMutableArray array];
    for (UIView<CXHAdapterNativeAdViewDelegate> *obj in adViewArray) {
        
        CXHAdapterNativeAdData *adData = [obj cxh_nativeAdData];
        
        CXHTopOnNativeObject *nativeObj = [[CXHTopOnNativeObject alloc]init];
        nativeObj.nativeAd = nativeAd;
        nativeObj.nativeAdRenderType = ATNativeAdRenderSelfRender;
        nativeObj.nativeAdView = obj;
        nativeObj.title = adData.title;
        nativeObj.mainText = adData.desc;
        nativeObj.imageUrl = adData.imageUrl;
        nativeObj.logoView = [obj cxh_logoView];
        nativeObj.iconUrl = adData.iconImageUrl;
        //设置为视频素材
        nativeObj.isVideoContents = false;
        
        [assets addObject:nativeObj];
    }
    [self.adStatusBridge atOnNativeAdLoadedArray:assets adExtra:infoDic];
}

/**
 信息流广告请求失败

 @param nativeAd 广告实例
 @param error 请求错误描述
 */
- (void)CXH_nativeAdFailToLoad:(CXHAdSDKNativeAd *)nativeAd error:(NSError *)error{
    [self.adStatusBridge atOnAdLoadFailed:error adExtra:nil];
}

/**
 信息流广告被点击

 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClicked:(CXHAdSDKNativeAd *)nativeAd
                     adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView loadingPageURL:(NSString *)loadingPageURL{
    [self.adStatusBridge atOnAdClick:nil];
}

/**
 信息流广告被关闭
 
 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClose:(CXHAdSDKNativeAd *)nativeAd
                   adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    [self.adStatusBridge atOnAdClosed:nil];
}


/**
 信息流广告被展示
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdExposure:(CXHAdSDKNativeAd *)nativeAd
                      adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    [self.adStatusBridge atOnAdShow:nil];
}

/**
 信息流广告展示上报
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdExposureReport:(CXHAdSDKNativeAd *)nativeAd
                            adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    
}

/**
 信息流广告点击上报

 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClickedReport:(CXHAdSDKNativeAd *)nativeAd
                           adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    
}

/**
 信息流广告落地页被关闭
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdCloseLandingPage:(CXHAdSDKNativeAd *)nativeAd
                              adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    [self.adStatusBridge atOnAdDetailClosed:nil];
}

@end
